package com.obelisco.vista.componentes;

import java.io.IOException;
import java.util.Vector;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.layout.ColumnLayoutData;
import nextapp.echo2.extras.app.layout.TabPaneLayoutData;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;

import tucana.echo2.app.FileUploadSelector;
import tucana.echo2.app.fileuploader.ProgressBar;
import tucana.echo2.app.fileuploader.UploadCallback;

import com.obelisco.vista.componentes.Ventana;
import com.obelisco.vista.controles.Label;
import com.obelisco.vista.estilos.Styles;
import com.obelisco.vista.imagen.ByteArrayImageReference;
import com.obelisco.vista.plantillas.VentanaBase;

import echopointng.ContainerEx;
import echopointng.ContentPaneEx;
import echopointng.ExtentEx;
import echopointng.ImageMap;
import echopointng.layout.DisplayLayoutData;

public class DialogoSubirArchivo extends VentanaBase {

	private PaneImagen paneImagen;

	private ContainerEx contenido;

	private byte[] fileData;

	private String contentType = "";

	private long fileSize = 0;

	private String fileName = "";

	private Vector<SubirArchivoListener> listeners2 = new Vector<SubirArchivoListener>();

	public DialogoSubirArchivo(String titulo) {
		super(titulo, TipoVentana.DIALOGO);
		initComponents();
	}

	private void initComponents() {
		Selector s = new Selector();
		s.getFileuploadSelector().setButtonDisplayMode(
				FileUploadSelector.BUTTON_DISPLAY_RIGHT);

		TabPaneLayoutData layout = new TabPaneLayoutData();
		layout.setTitle("Seleccionar Archivo ...");
		s.setLayoutData(layout);

		getAreaTrabajo().add(s);
	}

	@Override
	protected void accion(TipoAccion accion, TipoFuncion funcion, String nombre) {
		// TODO Auto-generated method stub
		if (accion == TipoAccion.ACEPTAR) {
			if (fileData != null)
				if (fileData.length > 0) {
					distribuirEventoSubirArchivo(fileData, contentType, fileName,
							fileSize);
				}
		}
		salir();
	}

	@Override
	protected void funcion(TipoFuncion funcion, String nombre) {
		// TODO Auto-generated method stub

	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public byte[] getFileData() {
		return fileData;
	}

	public void setFileData(byte[] fileData) {
		this.fileData = fileData;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public void addSubirArchivoListener(SubirArchivoListener listener) {
		listeners2.add(listener);
	}

	public void removeSubirArchivoListener(SubirArchivoListener listener) {
		listeners2.remove(listener);
	}

	protected void distribuirEventoSubirArchivo(byte[] data,
			String contentType, String fileName, long size) {
		for (SubirArchivoListener listener : listeners2) {
			listener.subirArchivo(data, contentType, fileName, size);
		}
	}

	class Selector extends ContentPaneEx {

		private FileUploadSelector fileuploadSelector;

		public Selector() {

			setInsets(new Insets(5));

			UploadCallback callback = new UploadCallback() {

				public void uploadFailed(
						tucana.echo2.app.fileuploader.UploadEvent arg0) {
					// TODO Auto-generated method stub

				}

				public void uploadSucceeded(
						tucana.echo2.app.fileuploader.UploadEvent event) {
					// TODO Auto-generated method stub
					try {
						ByteArrayOutputStream out = new ByteArrayOutputStream();
						IOUtils.copy(event.getInputStream(), out);
						event.getInputStream().close();
						byte[] fileBytes = out.toByteArray();
						final String ct = event.getContentType();

						setFileData(fileBytes);
						setFileName(event.getFileName());
						setFileSize(event.getFileSize());
						setContentType(ct);

						if (paneImagen == null) {
							paneImagen = new PaneImagen();
							paneImagen.setAlto(150);
							paneImagen.setAncho(150);
							paneImagen.setInsets(new Insets(
									new ExtentEx("10px")));
							contenido.add(paneImagen);
							Label lblSuccess = new Label();
							lblSuccess.setText("Archivo Cargado con Exito");
							contenido.add(lblSuccess);
							
						}

						if (ct.indexOf("image") >= 0) {
							paneImagen.setImagen(fileBytes, ct);
						}

					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			};

			fileuploadSelector = new FileUploadSelector();
			// fileuploadSelector.setWidthSize(40);
			// fileuploadSelector.setWidthExtent(new Extent(100,
			// Extent.PERCENT));
			// fileuploadSelector.setWidthMode(FileUploadSelector.WIDTH_MODE_EXTENT);
			fileuploadSelector.setUploadCallback(callback);

			fileuploadSelector
					.setButtonMode(FileUploadSelector.BUTTON_MODE_TEXT);

			contenido = new ContainerEx();

			contenido.add(fileuploadSelector);

			DisplayLayoutData layoutData = new DisplayLayoutData();
			layoutData.setHeight(new ExtentEx("95%"));
			layoutData.setWidth(new ExtentEx("95%"));
			layoutData.setInsets(new Insets(new ExtentEx("10px")));
			fileuploadSelector.setLayoutData(layoutData);

			ProgressBar p = new ProgressBar(fileuploadSelector);
			layoutData = new DisplayLayoutData();
			layoutData.setHeight(new ExtentEx("95%"));
			layoutData.setWidth(new ExtentEx("95%"));
			layoutData.setInsets(new Insets(new ExtentEx("10px")));
			layoutData.setAlignment(new Alignment(Alignment.CENTER,
					Alignment.CENTER));
			p.setLayoutData(layoutData);
			
		

			contenido.add(p);

			add(contenido);

		}

		public FileUploadSelector getFileuploadSelector() {
			return fileuploadSelector;
		}

		public void setFileuploadSelector(FileUploadSelector fileuploadSelector) {
			this.fileuploadSelector = fileuploadSelector;
		}
	}

}
